package com.siwei.eqi.util;

import com.siwei.eqi.pojo.entity.LandDataEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RegionBoundaryConverter {

    /**
     * 将 List<Map<String, String>> 转换为 List<LandDataEntity>
     *
     * @param regionBoundary 区域边界数据
     * @return 转换后的 List<LandDataEntity>
     */
    public static List<LandDataEntity> convertToLandDataEntityList(List<Map<String, String>> regionBoundary) {
        List<LandDataEntity> entityList = new ArrayList<>();

        if (regionBoundary == null || regionBoundary.isEmpty()) {
            return entityList; // 返回空列表，而不是 null
        }

        for (Map<String, String> data : regionBoundary) {
            LandDataEntity entity = new LandDataEntity();

            // 设置字段（确保 Map 中的 key 与 LandDataEntity 字段匹配）
            entity.setRegionCode(data.get("region_code"));
            entity.setBatchCode(data.get("batch_code"));

            // 进行数据类型转换
            entity.setForestArea(convertToBigDecimal(data.get("forest_area")));
            entity.setShrubForestArea(convertToBigDecimal(data.get("shrub_forest_area")));
            entity.setSparseForestArea(convertToBigDecimal(data.get("sparse_forest_area")));
            entity.setOtherForestArea(convertToBigDecimal(data.get("other_forest_area")));
            entity.setHighCoverageGrasslandArea(convertToBigDecimal(data.get("high_coverage_grassland_area")));
            entity.setMediumCoverageGrasslandArea(convertToBigDecimal(data.get("medium_coverage_grassland_area")));
            entity.setLowCoverageGrasslandArea(convertToBigDecimal(data.get("low_coverage_grassland_area")));
            entity.setRiverArea(convertToBigDecimal(data.get("river_area")));
            entity.setLakeArea(convertToBigDecimal(data.get("lake_area")));
            entity.setReservoirPondArea(convertToBigDecimal(data.get("reservoir_pond_area")));
            entity.setPermanentGlacierSnowArea(convertToBigDecimal(data.get("permanent_glacier_snow_area")));
            entity.setTidalFlatArea(convertToBigDecimal(data.get("tidal_flat_area")));
            entity.setFloodplainArea(convertToBigDecimal(data.get("floodplain_area")));
            entity.setCoastalSeaArea(convertToBigDecimal(data.get("coastal_sea_area")));
            entity.setPaddyFieldArea(convertToBigDecimal(data.get("paddy_field_area")));
            entity.setDrylandArea(convertToBigDecimal(data.get("dryland_area")));
            entity.setUrbanConstructionArea(convertToBigDecimal(data.get("urban_construction_area")));
            entity.setRuralSettlementArea(convertToBigDecimal(data.get("rural_settlement_area")));
            entity.setOtherConstructionArea(convertToBigDecimal(data.get("other_construction_area")));
            entity.setSandyArea(convertToBigDecimal(data.get("sandy_area")));
            entity.setGobiArea(convertToBigDecimal(data.get("gobi_area")));
            entity.setSalineAlkaliLandArea(convertToBigDecimal(data.get("saline_alkali_land_area")));
            entity.setSwampArea(convertToBigDecimal(data.get("swamp_area")));
            entity.setBareLandArea(convertToBigDecimal(data.get("bare_land_area")));
            entity.setBareRockGravelArea(convertToBigDecimal(data.get("bare_rock_gravel_area")));
            entity.setOtherArea(convertToBigDecimal(data.get("other_area")));
            entity.setTotalArea(convertToBigDecimal(data.get("total_area")));

            // 处理整数字段
            entity.setYear(convertToInteger(data.get("year")));
            entity.setRegionLevel(convertToInteger(data.get("region_level")));
            entity.setDataId(convertToInteger(data.get("data_id")));
            entity.setDataStatus(convertToInteger(data.get("data_status")));
            entity.setDataVersion(convertToInteger(data.get("data_version")));
            entity.setDataUpdateUser(convertToInteger(data.get("data_update_user")));

            // 处理时间字段
            entity.setDataUpdateTime(data.get("data_update_time"));

            // 添加到列表
            entityList.add(entity);
        }

        return entityList;
    }

    /**
     * 将字符串转换为 BigDecimal
     */
    private static BigDecimal convertToBigDecimal(Object value) {
        if (value == null) {
            return BigDecimal.ZERO;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (value instanceof String) {
            try {
                return new BigDecimal((String) value);
            } catch (NumberFormatException e) {
                return BigDecimal.ZERO;
            }
        }
        return BigDecimal.ZERO;
    }

    /**
     * 将字符串转换为 Integer
     */
    private static Integer convertToInteger(Object value) {
        if (value == null) {
            return 0;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (value instanceof String) {
            try {
                return Integer.parseInt((String) value);
            } catch (NumberFormatException e) {
                return 0;
            }
        }
        return 0;
    }

}
